﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using limagancalendar;
using model;

namespace limagan.shareclass
{
    public class limaganDataDAL
    {
        #region 数据库相关操作

        #region Calendar
        public List<Calendar> Find_Calendar_List_ByUID_and_TIME(string[] uid, string[] stime, string[] etime, int startIndex, int pageNum)
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                string mytempuid = " ";
                for (int i = 0; i < uid.Count(); i++)
                {
                    mytempuid += " ( logicdelete = 0 and UID = \"" + uid[i].ToString() + "\" and STime > \"" + stime[i].ToString() + "\" and STime <= \"" + etime[i].ToString() + "\") or ";
                }
                mytempuid += " UID = \"12345\" ";

                var query = (db.Calendar.Where(mytempuid)).Skip(startIndex).Take(pageNum);
                return query.ToList<Calendar>();
            }
        }

        public List<Calendar> Find_Calendar_List_ByUID_and_TIME(string[] uid, string[] mtime, int startIndex, int pageNum)
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                string mytempuid = " ";
                for (int i = 0; i < uid.Count(); i++)
                {
                    DateTime aaa = new DateTime(1, 1, 1, 0, 0, 0).AddSeconds(Convert.ToInt64(mtime[i].ToString()));
                    mytempuid += " ( logicdelete = 0 and UID = \"" + uid[i].ToString() + "\" and MTime > DateTime(" + aaa.ToString("yyyy,MM,dd,HH,mm,ss") + ") ) or ";
                }
                mytempuid += " UID = \"12345\" ";

                var query = (db.Calendar.Where(mytempuid)).Skip(startIndex).Take(pageNum);
                return query.ToList<Calendar>();
            }
        }

        public Calendar Find_Calendar_ByPara(string uid, string eid)
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                try
                {
                    Calendar my_lc = db.Calendar.Single(c => c.UID == uid && c.EID == eid && c.LogicDelete == 0);
                    return my_lc;
                }
                catch
                {
                    return null;
                }
            }
        }

        public List<Calendar> Find_Calendar_ByPara_list(string uid, string eid)
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                try
                {
                    var query = from c in db.Calendar
                                where c.UID == uid && c.EID == eid && c.LogicDelete == 0
                                select c;
                    return query.ToList<Calendar>();
                }
                catch
                {
                    return null;
                }
            }
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public string Update<T>(T entity, Action<T> update) where T : class
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                try
                {
                    db.GetTable<T>().Attach(entity);
                    update(entity);
                    db.SubmitChanges();
                    return "1";
                }
                catch
                {
                    return "0";
                }
            }
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string Insert<T>(T entity) where T : class
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                try
                {
                    db.GetTable<T>().InsertOnSubmit(entity);
                    db.SubmitChanges();
                    return "1";
                }
                catch
                {
                    return "0";
                }
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string Delete<T>(T entity) where T : class
        {
            using (dblimaganDataContext db = new dblimaganDataContext())
            {
                try
                {
                    db.GetTable<T>().Attach(entity);
                    db.GetTable<T>().DeleteOnSubmit(entity);
                    db.SubmitChanges();
                    return "1";
                }
                catch
                {
                    return "0";
                }
            }
        }
        #endregion

        #endregion
    }
}
